<template>
    <div>
        <el-form ref="form" :model="obj.data" label-width="80px">
            <el-form-item label="发生时间" prop="day" :rules="[formRulesMixin.notNull]">
                <DatePicker v-model:value="obj.data.day" />
            </el-form-item>
            <el-form-item label="收支类型" prop="type">
                <el-radio-group v-model="obj.data.type">
                    <el-radio v-for="x in globalData.financeTypeList" :key="x" :label="x.value">{{x.text}}</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="明细" prop="memo" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.memo" clearable autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="金额" prop="price" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.price" class="defalut-input" clearable autocomplete="off" />　元
            </el-form-item>
            <el-form-item class="dialog-btns-item">
                <div class="dialog-btns">
                    <span class="btn clear" @click="$emit('close')">取消</span>
                    <span class="btn sub ml10" @click.prevent="upData">提交</span>
                </div>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup>
    import { reactive,ref,getCurrentInstance,toRaw,inject } from 'vue'
    import DatePicker from '@/components/date_picker.vue'
    let { proxy } = getCurrentInstance()
    let obj = reactive({
        data:{
            finance_id: inject("id").value || 0,
            type: 0,
            memo: "",
            price:0,
        }
    })

    let init = async () => {
        if(obj.data.finance_id > 0){
            proxy.$ajax({
                url: 'finance/get',
                data: obj.data,
                bfLoading: true
            }).then(d => {
                if(d.success){
                    obj.data = {...obj.data, ...d.msg}
                    obj.data.type = parseInt(obj.data.type)
                    obj.data.price = parseFloat(obj.data.price)
                }else{
                    proxy.elMessageMixin(d.err, 'error')
                }
            }).catch(() => { })
        }
    }
    init()
    let upData = () => {
        proxy.$refs['form'].validate(valid => {
            if (valid) {
                proxy.$ajax({
                    url: 'finance/design',
                    data: obj.data,
                    bfLoading: true,
                }).then(d => {
                    if(d.success){
                        proxy.elMessageMixin(`财务记录${obj.data.id>0?'修改':"添加"}成功`, 'success')
                        proxy.$emit('init')
                        proxy.$emit('close')
                    }else{
                        proxy.elMessageMixin(d.err, 'error')
                    }
                }).catch(() => { })
            }else{
                return false
            }
        })
    }
</script>

<style scoped>
</style>